package com.xyg.controller;

import com.xyg.pojo.Product;
import com.xyg.pojo.Result;
import com.xyg.pojo.User;
import com.xyg.service.ProductService;
import com.xyg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping( "/api/admin" )
public class AdminController {

    private final ProductService productService;
    private final UserService userService;

    @Autowired
    public AdminController ( ProductService productService, UserService userService ) {
        this.productService = productService;
        this.userService = userService;
    }

    // todo 商品
    //* 获取商品列表: /api/admin/products
    @GetMapping( "/products" )
    public Result< List< Product > > getAllProducts () {
        List< Product > products = productService.findAll();
        return Result.success( "获取商品列表成功", products );
    }

    //* 添加商品: /api/admin/products
    @PostMapping( "/products" )
    public Result< Product > createProduct ( @RequestBody Product product ) {
        if ( product == null || product.getTitle() == null || product.getPrice() == null ) {
            return Result.failure( 400, "商品标题和价格不能为空" );
        }
        Product savedProduct = productService.save( product );
        return Result.success( "商品添加成功", savedProduct );
    }

    //* 查询商品: /api/admin/products/{id}
    @GetMapping( "/products/{id}" )
    public Result< Product > getProductById ( @PathVariable String id ) {
        if ( id == null || id.isEmpty() ) {
            return Result.failure( 400, "商品ID不能为空" );
        }
        return productService.findById( id )
                .map( product -> Result.success( "查询商品信息成功", product ) )
                .orElseGet( () -> Result.failure( 404, "商品信息未找到" ) );
    }

    //* 更新商品: /api/admin/products/{id}
    @PutMapping( "/products/{id}" )
    public Result< Product > updateProductById ( @PathVariable String id, @RequestBody Product productDetails ) {
        if ( productDetails == null || id == null || id.isEmpty() ) {
            return Result.failure( 400, "商品ID和更新内容不能为空" );
        }
        Product updatedProduct = productService.updateById( id, productDetails );
        return Result.success( "商品信息更新成功", updatedProduct );
    }

    //* 删除商品: /api/admin/products/{id}
    @DeleteMapping( "/products/{id}" )
    public Result< Void > deleteProductById ( @PathVariable String id ) {
        if ( id == null || id.isEmpty() ) {
            return Result.failure( 400, "商品ID不能为空" );
        }
        productService.deleteById( id );
        return Result.success( "商品删除成功", null );
    }

    //todo 用户
    //* 获取用户列表: /api/admin/users
    @GetMapping( "/users" )
    public Result< List< User > > getAllUsers () {
        List< User > users = userService.findAll();
        return Result.success( "获取用户列表成功", users );
    }

    //* 创建新用户: /api/admin/users
    @PostMapping( "/users" )
    public Result< User > createUser ( @RequestBody User user ) {
        if ( user == null || user.getUsername() == null ) {
            return Result.failure( 400, "用户名不能为空" );
        }
        user.setPassword( "123456" );
        User savedUser = userService.save( user );
        return Result.success( "用户创建成功", savedUser );
    }

    //* 根据ID查询用户信息: /api/admin/users/{id}
    @GetMapping( "/users/{id}" )
    public Result< User > getUserById ( @PathVariable String id ) {
        return userService.findById( id )
                .map( user -> Result.success( "查询用户信息成功", user ) )
                .orElseGet( () -> Result.failure( 404, "用户信息未找到" ) );
    }

    //* 更新用户信息: /api/admin/users/{id}
    @PutMapping( "/users/{id}" )
    public Result< User > updateUserById ( @PathVariable String id, @RequestBody User userDetails ) {
        if ( userDetails == null || id == null || id.isEmpty() ) {
            return Result.failure( 400, "用户ID和更新内容不能为空" );
        }
        User updatedUser = userService.updateById( id, userDetails );
        return Result.success( "用户信息更新成功", updatedUser );
    }

    //* 删除用户: /api/admin/users/{id}
    @DeleteMapping( "/users/{id}" )
    public Result< Void > deleteUserById ( @PathVariable String id ) {
        if ( id == null || id.isEmpty() ) {
            return Result.failure( 400, "用户ID不能为空" );
        }
        userService.deleteById( id );
        return Result.success( "用户删除成功", null );
    }

}
